package com.smec.remes.module.submit;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.smec.moblie.R;
import com.smec.moblie.databinding.ActivitySubmitBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.bean.ChangeStyle;
import com.smec.remes.module.submit.fragment.SmecFinishFragment;
import com.smec.remes.module.submit.fragment.SmecUnfinishFragment;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/6.
 */

public class SmecSubmitFragment extends SmecBaseFragment<SmecSubmitPresenter> {

    private ActivitySubmitBinding activitySubmitBinding;
    private ArrayList<Fragment> fragmentArrayList=new ArrayList<>();
    private ChangeStyle changeStyle=new ChangeStyle();
    public static final String SubmitFinish="SubmitFinish";
    public static final String unfinish="unfinish";
    public static final String finish="finish";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activitySubmitBinding =DataBindingUtil.inflate(inflater,R.layout.activity_submit,container,false);
        changeStyle.setOne(0);
        activitySubmitBinding.setChange(changeStyle);
        _setViewPager();
        _changeStyle();
        activitySubmitBinding.invalidateAll();
        return activitySubmitBinding.getRoot();
    }

    public void _setViewPager(){
        fragmentArrayList.add(SmecUnfinishFragment.newInstance());
        fragmentArrayList.add(SmecFinishFragment.newInstance());
        activitySubmitBinding.viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentArrayList.size();
            }
        });
    }

    public void _changeStyle(){
        activitySubmitBinding.btnYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                            changeStyle.setOne(0);
                            activitySubmitBinding.invalidateAll();
                            activitySubmitBinding.viewPager.setCurrentItem(0);
            }
        });

        activitySubmitBinding.btnNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeStyle.setOne(1);
                activitySubmitBinding.invalidateAll();
                activitySubmitBinding.viewPager.setCurrentItem(1);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        changeStyle.setOne(0);
        activitySubmitBinding.invalidateAll();
        activitySubmitBinding.viewPager.setCurrentItem(0);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(finish)
            }
    )

    public void _finish(String str){
        activitySubmitBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                changeStyle.setOne(1);
                activitySubmitBinding.invalidateAll();
                activitySubmitBinding.viewPager.setCurrentItem(1);
                mRxBus.post("RefrshFinish","aa");
            }
        },100);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(unfinish)
            }
    )

    public void _unfinish(String str){
        activitySubmitBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                changeStyle.setOne(0);
                activitySubmitBinding.invalidateAll();
                activitySubmitBinding.viewPager.setCurrentItem(0);
                mRxBus.post("RefrshNew","aa");
            }
        },100);
    }

    @Override
    protected SmecSubmitPresenter getPersenter() {
        return new SmecSubmitPresenter(getContext());
    }

    public static SmecSubmitFragment newInstance() {
        Bundle args = new Bundle();
        SmecSubmitFragment fragment = new SmecSubmitFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
